package com.wx.encrypt;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class EncryptTest {
    public static final String privateKey = "privateKey";
    public static final String aesKey = "privateKey";

    public void addWorker() throws Exception {
        // 方法的业务请求参数，放入map中
        Map<String, Object> map = new HashMap<>(8);
        map.put("cooperatorId", 122322222);
        map.put("timestamp", String.valueOf(System.currentTimeMillis()));
        map.put("workerName", "悠悠");
        map.put("workerMobile", "17600000000");
        map.put("idCard", "130000000000000000");
        map.put("workerType", "1");

        // 将非空参数按照参数名称按照ASCII升序排列(a=1&b=2&c=3)
        String sortParam = RSAUtils.sortParam(map);

        // 然后使用签名工具签名生成，RSA私钥加密
        String sign = RSAUtils.sign(sortParam.getBytes(), privateKey);
        map.put("sign", sign);

        // 转为json
        String jsonString = JSON.toJSONString(map);

        // AES加密

        String businessBody = AESUtils.encrypt2Hex(jsonString, aesKey);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("cooperatorId", 122322222);
        jsonObject.put("businessBody", businessBody);
//        String response = HttpUtils.postJson(BETA_SERVER_URL + WORKER_ADD, jsonObject.toJSONString());
//        System.out.println(response);

//        Map result = JSON.parseObject(response, Map.class);
//        if (result.get("data") != null) {
//            String data = String.valueOf(result.get("data"));
//            // 返回数据解密
//            String s = AESUtils.decryptByHex(data, aesKey);
//            Map map1 = JSON.parseObject(s, Map.class);
//            System.out.println(map1);
//        }
    }
}
